package com.beiye.drivertransport.fragment;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.frame.http.HttpListener;
import com.beiye.drivertransport.R;
import com.beiye.drivertransport.SubActivity.CourseDetailsActivity;
import com.beiye.drivertransport.adapter.CourseSuperAdatper;
import com.beiye.drivertransport.adapter.PopWindowDateApt;
import com.beiye.drivertransport.bean.CheckBean;
import com.beiye.drivertransport.bean.CheckDateBean;
import com.beiye.drivertransport.bean.CourseSuperBean;
import com.beiye.drivertransport.bean.LoginUserBean;
import com.beiye.drivertransport.http.Login;
import com.beiye.drivertransport.utils.CameraCanUseUtils;
import com.beiye.drivertransport.utils.TiShiDialog;
import com.beiye.drivertransport.utils.ToastUtil;
import com.beiye.drivertransport.utils.UserManger;
import com.beiye.drivertransport.utils.Utils;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;

import java.util.ArrayList;
import java.util.List;


import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 */
//驾驶员课程超市（这种界面暂时没有用到）
public class CourseSupermarketHomeFragment extends TwoBaseFgt {
    private int firstIndex = 1;
    private int pageSize = 10;


    TextView tv_course7;
//    @Bind(R.id.img_coursesupermarket)
//    TextView img_coursesupermarket;

    View empty_view;

    LRecyclerView lv_course;

    EditText ed_course;

    TextView tv_search;//搜索

    ImageView img_delet;//清除内容
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE"};
    private LRecyclerViewAdapter lRecyclerViewAdapter1;
    private CourseSuperAdatper courseSuperAdatper;

    private PopWindowDateApt popWindowDateApt;
    private PopupWindow mPopWindowDate;
    ArrayList<CheckDateBean.RowsBean> dateList = new ArrayList<>();

    @Override
    public int getLayoutId() {

        return R.layout.fragment_course_supermarket_home;
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void initData() {
        tv_course7 = (TextView) getActivity().findViewById(R.id.tv_course7);
        empty_view = (View) getActivity().findViewById(R.id.empty_view);
        lv_course = (LRecyclerView) getActivity().findViewById(R.id.lv_coursesupermarket);
        ed_course = (EditText) getActivity().findViewById(R.id.ed_course);
        tv_search = (TextView)getActivity().findViewById(R.id.tv_course);
        img_delet = (ImageView)getActivity().findViewById(R.id.img_delet);

        tv_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                lv_course.refresh();
                LoginUserBean userInfo = UserManger.getUserInfo();
                LoginUserBean.DataBean data = userInfo.getData();
                if (data == null) {
                    return;
                }
                String userId = data.getUserId();

                //存储一份readInterval文件
                SharedPreferences sp = getActivity().getSharedPreferences("readInterval4",
                        Context.MODE_PRIVATE);
                int readInterval = sp.getInt("readInterval", 0);
                //存储一份orgId文件
                SharedPreferences spp = getActivity().getSharedPreferences("orgId4",
                        Context.MODE_PRIVATE);
                String orgId = spp.getString("orgId", "");
                if (orgId.equals("")) {
                    lv_course.setEmptyView(empty_view);
                    lv_course.refreshComplete(0);
                    return;
                }
                String coursename = ed_course.getText().toString().trim();
                if (coursename.equals("")) {
                    new Login().getPreJobTrainingandcoursesupermaket(userId, orgId, "", 2, readInterval, firstIndex, pageSize, (HttpListener) getContext(), 1);
                } else {
                    new Login().getPreJobTrainingandcoursesupermaket(userId, orgId, coursename, 2, readInterval, firstIndex, pageSize, (HttpListener) getContext(), 1);
                }
            }
        });
        img_delet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ed_course.setText("");
                img_delet.setVisibility(View.GONE);
            }
        });



        //选中公司方法
        initCompany();
        initUi();

        ed_course.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //如果长度为0
                if (charSequence.length() == 0) {
                    img_delet.setVisibility(View.GONE);
                } else {
                    img_delet.setVisibility(View.VISIBLE);

                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        ed_course.setOnTouchListener(new View.OnTouchListener() {

            @SuppressLint("ClickableViewAccessibility")
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEvent.ACTION_DOWN == event.getAction()) {
                    ed_course.setCursorVisible(true);// 再次点击显示光标
                }
                return false;
            }
        });
    }

    private void initCompany() {

        String     orgName = getArguments().getString("orgName");
        String    orgId = getArguments().getString("orgId");
        int  minPer = getArguments().getInt("minPer");
        int  readInterval = getArguments().getInt("readInterval");
        int  signMark = getArguments().getInt("signMark");
        int  photoMark = getArguments().getInt("photoMark");
        int  mark = getArguments().getInt("Mark");

        tv_course7.setText(orgName);
        //存储一份readInterval文件
        SharedPreferences sp = getActivity().getSharedPreferences("orgId4",
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("orgId", orgId);
        editor.putInt("minPer", minPer);
        editor.commit();
        //存储一份readInterval文件
        SharedPreferences spp = getActivity().getSharedPreferences("readInterval4",
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor1 = spp.edit();
        editor1.putInt("readInterval", readInterval);
        editor1.commit();
        SharedPreferences sppp = getActivity().getSharedPreferences("Mark4", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor2 = sppp.edit();
        editor2.putInt("Mark", mark);
        editor2.commit();
        SharedPreferences sp1 = getActivity().getSharedPreferences("photoMarkandsignMark1",
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor3 = sp1.edit();
        editor3.putInt("photoMark",photoMark);
        editor3.putInt("signMark", signMark);
        editor3.commit();
        requestData();

    }

//    private void showPopupWindow() {
//        //设置contentView
//        View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.popwindow_layout, null);
//        mPopWindow = new PopupWindow(contentView,
//                WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, true);
//        //设置各个控件的点击响应
//        ListView lv_company = (ListView) contentView.findViewById(R.id.lv_company);
//        lv_company.setAdapter(popWindowApt);
//        mPopWindow.showAtLocation(tv_course, Gravity.CENTER, 0, 0);
//        lv_company.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
//                if (popWindowApt != null) {
//
//                    String orgName = popWindowApt.getItem(position).getOrgName();
//                    String orgId = popWindowApt.getItem(position).getOrgId();
//                    //        String adId = popWindowApt.getItem(position).getAdId();
//                    int minPer = popWindowApt.getItem(position).getMinPer();
//                    int readInterval = popWindowApt.getItem(position).getReadInterval();
//                    int mark = popWindowApt.getItem(position).getMark();
//                    int photoMark = popWindowApt.getItem(position).gettPhotoMark();
//                    int signMark = popWindowApt.getItem(position).gettSignMark();
//                    SharedPreferences sp1 = getActivity().getSharedPreferences("photoMarkandsignMark1",
//                            Context.MODE_PRIVATE);
//                    SharedPreferences.Editor editor3 = sp1.edit();
//                    editor3.putInt("photoMark",photoMark);
//                    editor3.putInt("signMark", signMark);
//                    editor3.commit();
//                    //存储一份readInterval文件
//                    SharedPreferences sp = getActivity().getSharedPreferences("orgId4",
//                            Context.MODE_PRIVATE);
//                    SharedPreferences.Editor editor = sp.edit();
//                    editor.putString("orgId", orgId);
//                    editor.putInt("minPer", minPer);
//                    editor.commit();
//                    //存储一份readInterval文件
//                    SharedPreferences spp = getActivity().getSharedPreferences("readInterval4",
//                            Context.MODE_PRIVATE);
//                    SharedPreferences.Editor editor1 = spp.edit();
//                    editor1.putInt("readInterval", readInterval);
//                    editor1.commit();
//                    SharedPreferences sppp = getActivity().getSharedPreferences("Mark4", Context.MODE_PRIVATE);
//                    SharedPreferences.Editor editor2 = sppp.edit();
//                    editor2.putInt("Mark", mark);
//                    editor2.commit();
//                    mPopWindow.dismiss();
//                    tv_course.setText(orgName);
//                    lv_course.refresh();
//                    requestData();
//
//                }
//            }
//        });
//    }


    //    //进入岗前培训时界面前调一下接口
//    public void readintervalData(String  adId)
//    {
////        String  adId =UserManger.getUserInfo().getAdId();
//        OkGo.get(AppInterfaceConfig.BASE_URL+"sys/config/query/"+adId)     // 请求方式和请求url
//                .tag(this)                       // 请求的 tag, 主要用于取消对应的请求
//                .execute(new StringCallback() {
//                    @Override
//                    public void onSuccess(String result, Call call, Response response) {
//
//                        ReadIntervalBean readIntervalBean =JSON.parseObject(result, ReadIntervalBean.class);
//
//                        ReadIntervalBean.DataBean data = readIntervalBean.getData();
//                        int   readInterval = data.getReadInterval();
//                        //存储一份readInterval文件
//                        SharedPreferences sp = getActivity().getSharedPreferences("readInterval",
//                                Context.MODE_PRIVATE);
//                        SharedPreferences.Editor editor = sp.edit();
//                        editor.putInt("readInterval", readInterval);
//                        editor.commit();
//                    }
//                });
//    }
    public void requestData() {

        LoginUserBean userInfo = UserManger.getUserInfo();
        LoginUserBean.DataBean data = userInfo.getData();
        if (data == null) {
            return;
        }
        String userId = data.getUserId();

        //存储一份readInterval文件
        SharedPreferences sp = getActivity().getSharedPreferences("readInterval4",
                Context.MODE_PRIVATE);
        int readInterval = sp.getInt("readInterval", 0);
        //存储一份orgId文件
        SharedPreferences spp = getActivity().getSharedPreferences("orgId4",
                Context.MODE_PRIVATE);
        String orgId = spp.getString("orgId", "");
        if (orgId.equals("")) {
            lv_course.setEmptyView(empty_view);
            lv_course.refreshComplete(0);
            return;
        }
        String coursename = ed_course.getText().toString().trim();
        if (coursename.equals("")) {
            new Login().getPreJobTrainingandcoursesupermaket(userId, orgId, "", 2, readInterval, firstIndex, pageSize, this, 1);
        } else {
            new Login().getPreJobTrainingandcoursesupermaket(userId, orgId, coursename, 2, readInterval, firstIndex, pageSize, this, 1);
        }

    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        if (what == 1) {
            CourseSuperBean courseSuperBean = JSON.parseObject(result, CourseSuperBean.class);
            if (courseSuperBean != null) {
                try {

                    if (courseSuperBean.getRows() != null && courseSuperBean.getRows().size() > 0) {
                        lv_course.setVisibility(View.VISIBLE);
                        if (firstIndex == 1) {
                            courseSuperAdatper.clear();
                            courseSuperAdatper.setDataList(courseSuperBean.getRows());
                        } else {
                            courseSuperAdatper.addAll(courseSuperBean.getRows());
                        }

                        if (courseSuperBean.getRows().size() < pageSize) {
                            lv_course.setNoMore(true);
                        }
                    } else {
                        if (firstIndex == 1) {//第一页
                            lv_course.setEmptyView(empty_view);
                            courseSuperAdatper.clear();
                        } else {
                            lv_course.setNoMore(true);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                lv_course.refreshComplete(courseSuperBean.getRows() == null ? 0 : courseSuperBean.getRows().size());
                lRecyclerViewAdapter1.notifyDataSetChanged();
            }
        } else if (what == 2) {
            CheckBean checkBean = JSON.parseObject(result, CheckBean.class);
            int code = checkBean.getCode();
            String msg = checkBean.getMsg();
            if (code == 0) {

                //存储一份readInterval文件
                SharedPreferences spp = getActivity().getSharedPreferences("courseSupermarktcSn1",
                        Context.MODE_PRIVATE);
                int tcSn = spp.getInt("tcSn", 0);
                Bundle bundle = new Bundle();
                bundle.putInt("tcsn", tcSn);
                SharedPreferences sp = getActivity().getSharedPreferences("readInterval4",
                        Context.MODE_PRIVATE);
                int readInterval = sp.getInt("readInterval", 0);
                bundle.putInt("readInterval", readInterval);
                //存储一份orgId文件
                SharedPreferences sppp = getActivity().getSharedPreferences("orgId4",
                        Context.MODE_PRIVATE);
                String orgId = sppp.getString("orgId", "");
                int minPer = sppp.getInt("minPer", 0);
                String setYm = sppp.getString("setYm", "");
                SharedPreferences sp1 = getActivity().getSharedPreferences("photoMarkandsignMark1",
                        Context.MODE_PRIVATE);
                int photoMark = sp1.getInt("photoMark", 0);
                int signMark = sp1.getInt("signMark", 0);
                bundle.putString("orgId", orgId);
                bundle.putInt("minPer", minPer);
                bundle.putString("setYm", setYm);
                bundle.putInt("photoMark",photoMark);
                bundle.putInt("signMark",signMark);
                startActivity(CourseDetailsActivity.class, bundle);
                mPopWindowDate.dismiss();

            }
        } else if (what == 3) {

            CheckBean checkBean = JSON.parseObject(result, CheckBean.class);
            int code = checkBean.getCode();
            String msg = checkBean.getMsg();
            if (code == 0) {

                initcoursesupermaketCheckDateData();//判断是否未完成年月份
            }
        } else if (what == 4) {
            CheckDateBean checkDateBean = JSON.parseObject(result, CheckDateBean.class);
            List<CheckDateBean.RowsBean> rows = checkDateBean.getRows();
            dateList.clear();
            dateList.addAll(rows);
            if (dateList.size() == 0) {
                if (!CameraCanUseUtils.isCameraCanUse()) {
                    ToastUtil.showShortToast(getActivity(), "请到手机设置界面里找驾运宝允许开启照相");
                    return;
                }
                final TiShiDialog.Builder builder = new TiShiDialog.Builder(getContext());
                builder.setMessage("您已完成本月学习任务，继续浏览课程将不记录学时");
                builder.setTitle("提示:");
                builder.setPositiveButton("继续", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        //设置你的操作事项
                        //存储一份readInterval文件
                        showLoadingDialog("正在加载");
                        SharedPreferences spp = getActivity().getSharedPreferences("courseSupermarktcSn1",
                                Context.MODE_PRIVATE);
                        int tcSn = spp.getInt("tcSn", 0);
                        Bundle bundle = new Bundle();
                        bundle.putInt("tcsn", tcSn);
                        SharedPreferences sp = getActivity().getSharedPreferences("readInterval4",
                                Context.MODE_PRIVATE);
                        int readInterval = sp.getInt("readInterval", 0);
                        bundle.putInt("readInterval", readInterval);
                        //存储一份orgId文件
                        SharedPreferences sppp = getActivity().getSharedPreferences("orgId4",
                                Context.MODE_PRIVATE);
                        String orgId = sppp.getString("orgId", "");
                        int minPer = sppp.getInt("minPer", 0);
                        SharedPreferences sp1 = getActivity().getSharedPreferences("photoMarkandsignMark1",
                                Context.MODE_PRIVATE);
                        int photoMark = sp1.getInt("photoMark", 0);
                        int signMark = sp1.getInt("signMark", 0);
                        bundle.putString("orgId", orgId);
                        bundle.putInt("minPer", minPer);
                        bundle.putString("setYm", "");
                        bundle.putInt("photoMark",photoMark);
                        bundle.putInt("signMark",signMark);
                        startActivity(CourseDetailsActivity.class, bundle);
                    }
                });

                builder.setNegativeButton("取消",
                        new android.content.DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });

                builder.create().show();


            } else {

                popWindowDateApt = new PopWindowDateApt(getContext(), dateList, R.layout.popwindow_dateitem_layout);
                showPopupWindowDate(dateList);//弹出未完成时
            }

        }


        super.onSuccess(result, call, response, what);


    }

    private void showPopupWindowDate(List<CheckDateBean.RowsBean> rows) {

        //设置contentView
        View contentView = LayoutInflater.from(getContext()).inflate(R.layout.popwindowdate_layout, null);
        mPopWindowDate = new PopupWindow(contentView,
                WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, true);
        //设置各个控件的点击响应
        LinearLayout view_parent = (LinearLayout) contentView.findViewById(R.id.checkdate_le);
        ListView lv_date = (ListView) contentView.findViewById(R.id.lv_date);
        TextView tv_more = (TextView) contentView.findViewById(R.id.tv_checkdate2);
        TextView tv_back = (TextView) contentView.findViewById(R.id.tv_checkdate4);
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPopWindowDate.dismiss();
            }
        });
        TextView tv_nocheckdate = (TextView) contentView.findViewById(R.id.tv_checkdate3);

        tv_nocheckdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (!CameraCanUseUtils.isCameraCanUse()) {
                    ToastUtil.showShortToast(getActivity(), "请到手机设置界面里找驾运宝允许开启照相");
                    return;
                }

                showLoadingDialog("正在加载");
                //存储一份readInterval文件
                SharedPreferences spp = getActivity().getSharedPreferences("courseSupermarktcSn1",
                        Context.MODE_PRIVATE);
                int tcSn = spp.getInt("tcSn", 0);
                Bundle bundle = new Bundle();
                bundle.putInt("tcsn", tcSn);
                SharedPreferences sp = getActivity().getSharedPreferences("readInterval4",
                        Context.MODE_PRIVATE);
                int readInterval = sp.getInt("readInterval", 0);
                bundle.putInt("readInterval", readInterval);
                //存储一份orgId文件
                SharedPreferences sppp = getActivity().getSharedPreferences("orgId4",
                        Context.MODE_PRIVATE);
                String orgId = sppp.getString("orgId", "");
                int minPer = sppp.getInt("minPer", 0);
                SharedPreferences sp1 = getActivity().getSharedPreferences("photoMarkandsignMark1",
                        Context.MODE_PRIVATE);
                int photoMark = sp1.getInt("photoMark", 0);
                int signMark = sp1.getInt("signMark", 0);
                bundle.putString("orgId", orgId);
                bundle.putInt("minPer", minPer);
                bundle.putString("setYm", "");
                bundle.putInt("photoMark", photoMark);
                bundle.putInt("signMark", signMark);
                startActivity(CourseDetailsActivity.class, bundle);
                mPopWindowDate.dismiss();
            }
        });
        mPopWindowDate.showAtLocation(view_parent, Gravity.CENTER, 0, 0);
        if (rows.size() >= 3) {
            View listItem = popWindowDateApt.getView(0, null, lv_date);
            listItem.measure(0, 0);
            int totalHei = (listItem.getMeasuredHeight() + lv_date.getDividerHeight()) * 3;
            lv_date.getLayoutParams().height = totalHei;
            lv_date.setAdapter(popWindowDateApt);
            tv_more.setVisibility(View.VISIBLE);
        } else {
            lv_date.setAdapter(popWindowDateApt);
            tv_more.setVisibility(View.INVISIBLE);
        }

        lv_date.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
//
//                if(dataList.size()==1) {
//                    showLoadingDialog("正在加载");
//                    String setYm = popWindowDateApt.getItem(position).getSetYm();
//                    //存储一份readInterval文件
//                    SharedPreferences spp = getActivity().getSharedPreferences("courseSupermarktcSn1",
//                            Context.MODE_PRIVATE);
//                    int tcSn = spp.getInt("tcSn", 0);
//                    Bundle bundle = new Bundle();
//                    bundle.putInt("tcsn", tcSn);
//                    SharedPreferences sp = getActivity().getSharedPreferences("readInterval4",
//                            Context.MODE_PRIVATE);
//                    int readInterval = sp.getInt("readInterval", 0);
//                    bundle.putInt("readInterval", readInterval);
//                    //存储一份orgId文件
//                    SharedPreferences sppp = getActivity().getSharedPreferences("orgId4",
//                            Context.MODE_PRIVATE);
//                    String orgId = sppp.getString("orgId", "");
//                    int minPer = sppp.getInt("minPer", 0);
//                    bundle.putString("orgId", orgId);
//                    bundle.putInt("minPer", minPer);
//                    bundle.putString("setYm", setYm);
//                    startActivity(CourseDetailsActivity.class, bundle);
//                    mPopWindowDate.dismiss();
//                }else

                if (dateList.size() >= 1) {
                    if (position == 0) {
                        showLoadingDialog("正在加载");
                        String setYm = popWindowDateApt.getItem(0).getSetYm();
                        SharedPreferences sp = getActivity().getSharedPreferences("orgId4",
                                Context.MODE_PRIVATE);
                        SharedPreferences.Editor edit = sp.edit();
                        edit.putString("setYm", setYm);
                        edit.commit();
                        String orgId = sp.getString("orgId", "");
                        initspeciacoursesupermaketCheckData(setYm, orgId);
                    } else {

                        TiShiDialog.Builder builder1 = new TiShiDialog.Builder(getContext());
                        builder1.setMessage("请先学习上月或以往未完成的课程");
                        builder1.setTitle("提示:");
                        builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                ;
                                //设置你的操作事项

                            }
                        });
                        builder1.create().show();
                    }
                }
            }
        });

    }

    //补学弹窗校验
    private void initcoursesupermaketCheckDateData() {

        //存储一份orgId文件
        SharedPreferences spp = getActivity().getSharedPreferences("orgId4",
                Context.MODE_PRIVATE);
        String orgId = spp.getString("orgId", "");
        LoginUserBean userInfo = UserManger.getUserInfo();
        LoginUserBean.DataBean data = userInfo.getData();
        if (data == null) {
            return;
        }
        String userId = data.getUserId();
        new Login().getcoursesupermaketCheckDate(userId, orgId, this, 4);

    }

    @Override
    public void onError(String result, Call call, Response response, int what) {
        if (what == 2) {
            CheckBean checkBean = JSON.parseObject(result, CheckBean.class);
            int code = checkBean.getCode();
            if (code == 2813) {
                String msg = checkBean.getMsg();
                TiShiDialog.Builder builder1 = new TiShiDialog.Builder(getContext());
                builder1.setMessage(msg);
                builder1.setTitle("提示:");
                builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        ;
                        //设置你的操作事项
                        mPopWindowDate.dismiss();
                    }
                });
                builder1.create().show();

            }
        } else if (what == 3) {

            CheckBean checkBean = JSON.parseObject(result, CheckBean.class);
            int code = checkBean.getCode();
            if (code == 1812) {
                String msg = checkBean.getMsg();
                TiShiDialog.Builder builder1 = new TiShiDialog.Builder(getContext());
                builder1.setMessage(msg);
                builder1.setTitle("提示:");
                builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        ;
                        //设置你的操作事项
                    }
                });
                builder1.create().show();
            }
        }
        super.onError(result, call, response, what);
    }

    @Override
    public void onResume() {
        super.onResume();
        lv_course.refresh();
        requestData();
    }

    @Override
    public void onUserVisible() {

    }

    private void initUi() {
        LinearLayoutManager ms = new LinearLayoutManager(getActivity());
        ms.setOrientation(LinearLayoutManager.VERTICAL);
        lv_course.setLayoutManager(ms);
        courseSuperAdatper = new CourseSuperAdatper(getContext());
        lRecyclerViewAdapter1 = new LRecyclerViewAdapter(courseSuperAdatper);
        lv_course.setAdapter(lRecyclerViewAdapter1);
        lv_course.setFooterViewColor(R.color.colorAccent, R.color.dark, android.R.color.white);
        lv_course.setFooterViewHint("拼命加载中", "已经全部为你呈现了", "网络不给力啊，点击再试一次吧");
        lv_course.setOnRefreshListener(new OnRefreshListener() {

            @Override
            public void onRefresh() {
                firstIndex = 1;
                requestData();
            }
        });

        lv_course.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void onLoadMore() {
                firstIndex += pageSize;
                requestData();
            }
        });
        empty_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lv_course.refresh();
            }
        });

        lRecyclerViewAdapter1.setOnItemClickListener(new com.github.jdsjlzx.interfaces.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (Utils.isFastDoubleClick()) {
                    return;
                }

                //检测是否有写的权限
                int permission = ActivityCompat.checkSelfPermission(getContext(),
                        "android.permission.WRITE_EXTERNAL_STORAGE");
                if (permission != PackageManager.PERMISSION_GRANTED) {
                    // 没有写的权限，去申请写的权限，会弹出对话框
                    ActivityCompat.requestPermissions(getActivity(), PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
                    return;
                }

                try {
                    SharedPreferences sp1 = getActivity().getSharedPreferences("Mark4", Context.MODE_PRIVATE);
                    int mark = sp1.getInt("Mark", 1);
                    if (mark == 0) {
                        TiShiDialog.Builder builder1 = new TiShiDialog.Builder(getActivity());
                        builder1.setMessage("您已被企业管理员禁用,请联系企业管理员");
                        builder1.setTitle("提示:");
                        builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                //设置你的操作事项
                            }
                        });
                        builder1.create().show();
                        return;
                    }
                    //校验是否学过课程
                    int tcSn = courseSuperAdatper.getDataList().get(position).getTcSn();
                    //存储一份tcSn文件
                    SharedPreferences sp = getActivity().getSharedPreferences("courseSupermarktcSn1",
                            Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putInt("tcSn", tcSn);
                    editor.commit();
                    //      initspeciacoursesupermaketCheckData();
                    initcoursesupermaketCheckData();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        lv_course.refresh();

    }

    //    //校验指定学习是否学过
    private void initspeciacoursesupermaketCheckData(String setYm, String orgId) {
        LoginUserBean userInfo = UserManger.getUserInfo();
        LoginUserBean.DataBean data = userInfo.getData();
        if (data == null) {
            return;
        }
        String userId = data.getUserId();
//        //存储一份orgId文件
//        SharedPreferences sp = getActivity().getSharedPreferences("orgId4",
//                Context.MODE_PRIVATE);
//        String orgId = sp.getString("orgId", "");
        new Login().getlearncoursesupermaketCheck(userId, orgId, setYm, this, 2);
    }

    //    //校验课程超市课程是否学过
    private void initcoursesupermaketCheckData() {
        LoginUserBean userInfo = UserManger.getUserInfo();
        LoginUserBean.DataBean data = userInfo.getData();
        if (data == null) {
            return;
        }
        String userId = data.getUserId();
        //存储一份readInterval文件
        SharedPreferences sp = getActivity().getSharedPreferences("readInterval4",
                Context.MODE_PRIVATE);
        int readInterval = sp.getInt("readInterval", 0);
        //存储一份readInterval文件
        SharedPreferences spp = getActivity().getSharedPreferences("courseSupermarktcSn1",
                Context.MODE_PRIVATE);
        int tcSn = spp.getInt("tcSn", 0);
        //存储一份orgId文件
        SharedPreferences sppp = getActivity().getSharedPreferences("orgId4",
                Context.MODE_PRIVATE);
        String orgId = sppp.getString("orgId", "");
//        dialog = CustomProgressDialog.show(getContext(), "", true, null);
        new Login().getPreJobTrainingandcoursesupermaketCheck(userId, orgId, tcSn, readInterval, this, 3);

    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return false;
    }
}
